<?php
function show_console() {
?>
<style type="text/css">
#pConsole {
	position: fixed;
	left: 0;
	top: 0;
	width: 100%;
	background: none;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
	z-index: 10000;
	
	display:none;
}

#pConsole .pConsole_bar {
	background: #222;
	border-bottom: 1px solid #888;
	color: #ccc;
}

#pConsole .pConsole_bar ul {
	list-style-position: inside;
	list-style-type: none;
	margin: 0;
	padding: 0;
}

#pConsole .pConsole_bar ul li {
	float: left;
}

#pConsole .pConsole_bar ul li a {
	padding: 5px 10px;
	display: block;
	
	font-weight: bold;
	color: #ccc;
	text-decoration: none;
}

#pConsole .pConsole_bar ul li a:hover {
	background: #FFB62E;
	color: #222;
}

#pConsole .pConsole_tabs {
	height: 400px;
	clear: both;
	background: #000;
	opacity:0.80; /* firefox, opera, safari, chrome */
    -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(opacity=80)"; /* IE 8 */
    filter:alpha(opacity=80); 
}

#pConsole .pConsole_tab {
	color: #fff;
	display: none;
	overflow: auto;
	height: 100%;
}

#pConsole .pConsole_bottomBar {
	min-height: 5px;
	max-height: 5px;
	height: 5px;
	cursor: s-resize;
}

#pConsole .out_row {
	padding: 2px 0;
	border-bottom: 1px solid #909090;
}

#pConsole .out_name {
	color: #fff;
}

#pConsole .out_value {
	color: yellow;
}

</style>

<script type="text/javascript">
PConsole = {
	_console: null,
	_buttons: null,
	_tabs: null,

	_tabsContainer: null,

	_openedTab: null,
	_ctrKeyDown: false,
		
	init: function() {
		var that = this;
		
		this._console = jQuery("#pConsole");
		this._tabsContainer = jQuery('#pConsole .pConsole_tabs');
		this._buttons = this._console.find('.pConsole_bar a');
		this._tabs = this._console.find('.pConsole_tabs .pConsole_tab');

		if (this._buttons.length != this._tabs.length) return;

		this._openedTab = jQuery(this._tabs[0]);
		this._openedTab.css('display', 'block');

		jQuery(this._buttons).each(function(idx, el) {
			jQuery(el).click(function() {
				that.show(that._tabs[idx]);
			});
		});

		jQuery('#pConsole .pConsole_bottomBar').mousedown(function(e) {
			that.resize(e);
		});
		
		jQuery(document).mouseup(function() {
			jQuery(document).unbind('mousemove');
		});

		jQuery(document).keydown(function(e) {
			if (e.keyCode == 17) {
				that._ctrKeyDown = true;
				e.preventDefault();
				return;
			}

			if (e.keyCode == 192 && that._ctrKeyDown) {
				that._open();
				e.preventDefault();
				return;
			}
		});
		jQuery(document).keyup(function(e) {
			if (e.keyCode == 17) {
				that._ctrKeyDown = false;
				e.preventDefault();
				return;
			}
		});
	},

	show: function(tab) {
		if (this._openedTab == tab) return;

		if (this._openedTab != null) {
			this._openedTab.hide();
		}

		this._openedTab = jQuery(tab);
		this._openedTab.show();
	},

	resize: function(ev) {
		var that = PConsole;
		var ih = that._tabsContainer.height();
		
		jQuery(document).bind('mousemove', function(e) {
			if (jQuery(this).height() - 100 < e.pageY) {
				return;
			}

			that._tabsContainer.height(ih + e.pageY - ev.pageY);
		});
	},

	_open: function() {
		var console = PConsole._console;
		
		if (console.css('display') == 'none') {
			console.slideDown();
		} else {
			console.slideUp();
		}
	}
};

jQuery(document).ready(function() {
	PConsole.init();
});
</script>

<?php 
$visitor = Security_Manager::get_visitor();
$sql_data = Console::get_database_data();
?>

<div id="pConsole">
	<div class='pConsole_bar'>
		<ul>
			<li><a href="javascript:void(0)">Logs</a></li>
			<li><a href="javascript:void(0)">Dumps</a></li>
			<li><a href="javascript:void(0)">App</a></li>
			<li><a href="javascript:void(0)">GET</a></li>
			<li><a href="javascript:void(0)">POST</a></li>
			<li><a href="javascript:void(0)">REQUEST</a></li>
			<li><a href="javascript:void(0)">SERVER</a></li>
			<li><a href="javascript:void(0)">SQL</a></li>
			<li><a href="javascript:void(0)">Visitor</a></li>
		</ul>
		<div style="clear:both"></div>
	</div>
	<div class='pConsole_tabs'>
		<div class="pConsole_tab" style="padding: 10px; 20px;">
			<?php 
			foreach (Console::get_logs() as $log) : ?>
			<div class="out_row">
				<span class="out_name">[<?=strtoupper($log['level'])?>]</span>
				<span class="out_value"><?=$log['log']?></span>
			</div>
			<?php endforeach ?>
		</div>
		<div class="pConsole_tab" style="padding: 10px; 20px;">
			<?php 
			foreach (Console::get_dumps() as $var) : ?>
			<div class="out_row">
				<span class="out_value"><?php var_dump($var)?></span>
			</div>
			<?php endforeach ?>
		</div>
		<div class="pConsole_tab" style="padding: 10px; 20px;">
			<div class="out_row">
				<span class="out_name">Memory usage</span>
				<span class="out_value">{memory_usage}</span>
			</div>
			<div class="out_row">
				<span class="out_name">Ellapsed time</span>
				<span class="out_value">{elapsed_time}</span>
			</div>
		</div>
		<div class="pConsole_tab" style="padding: 10px; 20px;">
			<div>
				<?php if (empty($_GET)) : ?>
				<span class="out_value"><b>No data</b></span>
				<?php else :?>
					<?php foreach ($_GET as $key => $value) : ?>
					<div class="out_row">
						<span class="out_name"><?=$key?>: </span>
						<span class="out_value"><?=$value?></span>
					</div>
					<?php endforeach ?>
				<?php endif; ?>
			</div>
		</div>
		<div class="pConsole_tab" style="padding: 10px; 20px;">
			<div>
				<?php if (empty($_POST)) : ?>
				<span class="out_value"><b>No data</b></span>
				<?php else :?>
					<?php foreach ($_POST as $key => $value) : ?>
					<div class="out_row">
						<span class="out_name"><?=$key?>: </span>
						<span class="out_value"><?=$value?></span>
					</div>
					<?php endforeach ?>
				<?php endif; ?>
			</div>
		</div>
		<div class="pConsole_tab" style="padding: 10px; 20px;">
			<div>
				<?php if (empty($_REQUEST)) : ?>
				<span class="out_value"><b>No data</b></span>
				<?php else :?>
					<?php foreach ($_REQUEST as $key => $value) : ?>
					<div class="out_row">
						<span class="out_name"><?=$key?>: </span>
						<span class="out_value"><?=$value?></span>
					</div>
					<?php endforeach ?>
				<?php endif; ?>
			</div>
		</div>
		<div class="pConsole_tab" style="padding: 10px; 20px;">
			<div>
				<?php if (empty($_SERVER)) : ?>
				<span class="out_value"><b>No SERVER data</b></span>
				<?php else :?>
					<?php foreach ($_SERVER as $key => $value) : ?>
					<div class="out_row">
						<span class="out_name"><?=$key?>: </span>
						<span class="out_value"><?=$value?></span>
					</div>
					<?php endforeach ?>
				<?php endif; ?>
			</div>
		</div>
		<div class="pConsole_tab" style="padding: 10px; 20px;">
			<?php foreach ($sql_data as $key => $value) : ?>
			<div class="out_row">
				<span class="out_name"><?=$key?>: </span>
				<?php if (in_array($key, array('queries', 'query_times', 'data_cache'))) :?>
					<?php foreach ($value as $k => $v) : ?>
					<div class="out_row">
						<span class="out_value"><?=$v?></span>
					</div>
					<?php endforeach;?>
				<?php else : ?>
				<span class="out_value"><?=$value?></span>
				<?php endif; ?>
			</div>
			<?php endforeach ?>
		</div>
		<div class="pConsole_tab">
			<div style="padding: 10px; 20px;">
				<?php
				if (empty($visitor->id)) : ?>
				<div class="out_row">
					<span class="out_value">GUEST</span>
				</div>
				<?php else : ?>
				<div class="out_row">
					<span class="out_name">ID: </span>
					<span class="out_value"><?=$visitor->id?></span>
				</div>
				<div class="out_row">
					<span class="out_name">User: </span>
					<span class="out_value"><?=$visitor->email?></span>
				</div>
				<?php endif; ?>
				<div class="out_row">
					<span class="out_name">Roles: </span>
					<span class="out_value">
						<?php foreach ($visitor->roles as $role) : ?>
						<?=$role?>&nbsp;
						<?php endforeach; ?>
					</span>
				</div>
			</div>
		</div>
	</div>
	<div class="pConsole_bottomBar"></div>
</div>
<?php 
}